package rabbit.flt.plugins.springmvc;

import java.io.PrintWriter;
import java.io.Writer;

public class PrintWriterWrapper extends PrintWriter {

    private ServletResponseWrapper wrapper;

    public PrintWriterWrapper(Writer writer, ServletResponseWrapper wrapper) {
        super(writer);
        this.wrapper = wrapper;
    }

    @Override
    public void write(String s, int off, int len) {
        super.write(s, off, len);
        this.wrapper.getCallBack().accept(s.substring(off, off + len));
    }

    @Override
    public void write(char[] buf, int off, int len) {
        super.write(buf, off, len);
        char[] dest = new char[len];
        System.arraycopy(buf, off, dest, 0, len);
        this.wrapper.getCallBack().accept(new String(dest));
    }
}
